package com.jushu.storbox.adapter;

import java.io.File;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.jushu.storbox.R;
import com.jushu.storbox.util.DirectoryUtil;
import com.jushu.storbox.util.OnImageDownload;
import com.jushu.storbox.util.PublicTools;
import com.jushu.storbox.util.image.IconVerification;
import com.jushu.storbox.util.image.ImageDownloader;
import com.jushu.storbox.view.viewImpl.MyImageView;

public class FolderBrowserAdapter extends BaseAdapter
{
    private List<File> files = null;
    
    private Context context;
    
    private ListView listView;
    
    private ImageDownloader mDownloader;
    
    private ViewHolder holder;
    
    public ViewHolder getHolder()
    {
        return holder;
    }
    
    public FolderBrowserAdapter(List<File> files, ListView listView, Context context)
    {
        this.files = files;
        this.context = context;
        this.listView = listView;
    }
    
    @Override
    public int getCount()
    {
        return files.size();
    }
    
    @Override
    public File getItem(int position)
    {
        return files.get(position);
    }
    
    @Override
    public long getItemId(int position)
    {
        return position;
    }
    
    @SuppressLint("SimpleDateFormat")
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        if (null == convertView)
        {
            convertView = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.selfolder_listview_item,
                null);
            holder = new ViewHolder();
            holder.iv = (MyImageView) convertView.findViewById(R.id.iv_icon);
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.file_size = (TextView) convertView.findViewById(R.id.upload_file_size);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.item = new File(this.getItem(position).getPath());
        holder.tv_name.setText(holder.item.getName());
//        holder.file_size.setText(PublicTools.changeBKM(String.valueOf(holder.item.length())));
        if (holder.item.isDirectory())
        {
            holder.iv.setImageResource(R.mipmap.folder_im);
            holder.file_size.setVisibility(View.INVISIBLE);
        }
        else
        {
            holder.file_size.setVisibility(View.VISIBLE);
            holder.file_size.setText(PublicTools.changeBKM(String.valueOf(holder.item.length())));
        }
        setFileTypeImage(holder, position);
        return convertView;
    }
    
    private void setFileTypeImage(ViewHolder viewHolder, int itemID)
    {

        String iconPath = DirectoryUtil.generateLocalFileThumbnailPath(context,files.get(itemID).getName());
        viewHolder.iv.setTag(iconPath);
        viewHolder.iv.setImageResource(R.mipmap.default_file_icon);
        if (files.get(itemID).isDirectory())
        {
            viewHolder.iv.setImageResource(R.mipmap.folder_im);
        }
        else
        {
            if (IconVerification.checkEndsWithInStringArray(files.get(itemID).getName(),
                context.getResources().getStringArray(R.array.fileEndingImage)))
            {
                holder.iv.setTag(files.get(itemID).getPath());
                if (mDownloader == null)
                {
                    mDownloader = new ImageDownloader();
                }
                holder.iv.setImageResource(R.mipmap.default_file_icon);
                if (mDownloader != null)
                {
                    mDownloader.imageDownload(files.get(itemID).getPath(),
                        holder.iv,
                        null,
                        2,
                        "/storage/emulated/0/CloudDrive/thumbnail",
                        context,
                        new OnImageDownload()
                        {
                            @Override
                            public void onDownloadSucc(Bitmap bitmap, String c_url, ImageView mimageView)
                            {
                                ImageView imageView = (ImageView) listView.findViewWithTag(c_url);
                                if (imageView != null)
                                {
                                    imageView.setImageBitmap(bitmap);
                                    imageView.setTag("");
                                }
                            }
                        });
                }
            }
            else
            {
            	String thumnailPath = DirectoryUtil.generateLocalFileThumbnailPath(context,files.get(itemID).getName());
                IconVerification.fileIconClass(context, listView, viewHolder.iv, 0, files.get(itemID)
                    .getName(), false, thumnailPath, null);
            }
        }
    }
    
    public class ViewHolder
    {
        public MyImageView iv;
        
        public TextView tv_name;
        
        public TextView file_size;
        
        public File item = null;
    }
}
